import { defineStore } from 'pinia';
import { ref } from 'vue';

export type ThemeType = 'light' | 'dark';
export type LanguageType = 'zh-CN' | 'en-US';

export const useAppStore = defineStore(
  'app',
  () => {
    const theme = ref<ThemeType>('light');
    const language = ref<LanguageType>('zh-CN');
    const primaryColor = ref<string>('#007AFF');
    const loading = ref<boolean>(false);

    const setTheme = (newTheme: ThemeType) => {
      theme.value = newTheme;
      // 设置页面主题
      const pages = getCurrentPages();
      if (pages.length > 0) {
        const currentPage = pages[pages.length - 1];
        if (currentPage.$page && currentPage.$page.meta) {
          currentPage.$page.meta.isQuit = newTheme === 'dark';
        }
      }
    };

    const setLanguage = (newLanguage: LanguageType) => {
      language.value = newLanguage;
    };

    const setPrimaryColor = (color: string) => {
      primaryColor.value = color;
    };

    const setLoading = (isLoading: boolean) => {
      loading.value = isLoading;
    };

    const toggleTheme = () => {
      setTheme(theme.value === 'light' ? 'dark' : 'light');
    };

    return {
      theme,
      language,
      primaryColor,
      loading,
      setTheme,
      setLanguage,
      setPrimaryColor,
      setLoading,
      toggleTheme,
    };
  },
  {
    persist: {
      key: 'app-store',
      paths: ['theme', 'language', 'primaryColor'],
    },
  }
);